<?php

class NoteController extends Controller {

    public function actionCreateNote() {

        //get tab id
        //if doesn't exists , create
        if($_POST['tab_id']){
            $Tab = Tabs::model()->findByPk($_POST['tab_id']);
        }
        else{
            $Tab = new Tabs();
            $Tab->title = 'Default';

            if(Yii::app()->user->isGuest){
                $Tab->session_id = $_POST['sid'];
                $Tab->user_id = 0;
            }
            else{
                $Tab->session_id = 0;
                $Tab->user_id = Yii::app()->user->id;
            }
            $Tab->save();
        }



        $Note = new Notes();

        $Note->pos_x = $_POST['pos_x'];
        $Note->pos_y = $_POST['pos_y'];
        $Note->font = $_POST['font'];
        $Note->content = nl2br(strip_tags($_POST['content']));
        $Note->pos_z = $_POST['pos_z'];
        $Note->tab_id = $Tab->id;
                    
        //	$Note->user_id = (Yii::app()->user->id?Yii::app()->user->id:$_GET['s']);
        $Note->insert();

        $this->renderPartial('note', array('Note'=>$Note), false, true);

    }


    /**
     * Remove note from database
     */
    public function actionDelete(){
        //@todo: sprawdzenie czy mozna usunać notke, czuy nalezy do tego użytkownika
        if(Yii::app()->request->isAjaxRequest){
            Notes::model()->deleteByPk($_POST['id']);
        }
    }

    /**
     * Update note position
     */
    public function actionUpdate(){
        if(Yii::app()->request->isAjaxRequest){
            Notes::model()->updateByPk($_POST['id'], $_POST);
        }
    }


}